package com.example.springboot.controller;

import com.example.springboot.common.Page;
import com.example.springboot.common.Result;
import com.example.springboot.entity.User;
import com.example.springboot.service.UserService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能：
 * 作者：ljr
 * 日期：2023/11/3 10:04
 */
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 增加用户信息
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody User user) {
        try{
            userService.insertUser(user);
        }catch (Exception e){
            return Result.error("增加信息错误");
        }
        return Result.success();
    }

    /**
     * 修改用户信息
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody User user) {
        try{
            userService.updateUser(user);
        }catch (Exception e){
            return Result.error("修改信息错误");
        }
        return Result.success();
    }

    /**
     * 删除用户信息
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        try{
            userService.deleteUser(id);
        }catch (Exception e){
            return Result.error("删除信息错误");
        }
        return Result.success();
    }

    /**
     * 批量删除用户信息
     * @return
     */
    @DeleteMapping("/delete/batch")
    public Result batchdelete(@RequestBody List<Integer> ids) {
        try{
            userService.batchdeleteUser(ids);
        }catch (Exception e){
            return Result.error("删除信息错误");
        }
        return Result.success();
    }

    /**
     * 查询全部的用户信息
     * @return
     */
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<User> userList;
        try {
            userList = userService.selectAll();
        } catch (Exception e) {
            return Result.error("查询信息错误");
        }
        return Result.success(userList);
    }

    /**
     * 根据主键查询用户信息
     * @return
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        User user;
        try {
            user = userService.selectById(id);
        } catch (Exception e) {
            return Result.error("查询信息错误");
        }
        return Result.success(user);
    }

    /**
     * 多条件查询用户信息
     * @return
     */
    @GetMapping("/selectByMore")
    public Result selectByMore(@RequestParam String name,@RequestParam String username) {
        List<User> userList;
        try {
            userList = userService.selectByMore(name,username);
        } catch (Exception e) {
            return Result.error("查询信息错误");
        }
        return Result.success(userList);
    }

    /**
     * 多条件模糊查询用户信息
     * @return
     */
    @GetMapping("/selectByMo")
    public Result selectByMo(@RequestParam String name,@RequestParam String username) {
        List<User> userList;
        try {
            userList = userService.selectByMo(name,username);
        } catch (Exception e) {
            return Result.error("查询信息错误");
        }
        return Result.success(userList);
    }

    /**
     * 多条件分页查询用户信息
     * @return
     */
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize,
                               @RequestParam String name,@RequestParam String username) {
        Page<User> pageMap = userService.selectByPage(pageNum,pageSize,name,username);

        return Result.success(pageMap);
    }

}
